package com.github.ecbp.user.service.controller.front;

import com.github.ecbp.common.api.CommonResult;
import com.github.ecbp.common.data.builder.CommonPageBuilder;
import com.github.ecbp.common.data.controller.BaseController;
import com.github.ecbp.common.data.utils.TransformUtils;
import com.github.ecbp.common.req.BaseIdDTO;
import com.github.ecbp.user.service.dto.ResourceQueryDto;
import com.github.ecbp.user.service.entity.TResource;
import com.github.ecbp.user.service.response.ResourceRes;
import com.github.ecbp.user.service.service.ResourceService;
import com.github.ecbp.user.service.vo.ResourcePageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.github.ecbp.common.constant.CacheKeyConstant.PROJECT_NAME;

/**
 * <p>
 * 后台资源表 前端控制器
 * </p>
 *
 * @author zj2626
 * @since 2020-03-18
 */
@RequestMapping("/api/" + PROJECT_NAME + "/resource")
@Api(tags = "后台资源管理")
@RestController
public class ResourceController extends BaseController {
    @Autowired
    protected ResourceService service;

    @ApiOperation(value = "所有可选数据下拉列表", notes = "zhangj", response = ResourceRes.class)
    @PostMapping(value = "/listAll")
    public CommonResult listAll() {
        return CommonResult.succeed(service.listAllData());
    }

    @ApiOperation(value = "根据ID获取资源详情", notes = "zhangj", response = ResourceRes.class)
    @PostMapping(value = "/info")
    public CommonResult getItem(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        TResource result = service.getById(param.getId());
        return CommonResult.succeed(TransformUtils.copy(result, ResourceRes.class));
    }

    @ApiOperation(value = "分页列表-后台资源", notes = "zhangj", response = ResourcePageVo.class)
    @PostMapping(value = "/selectPage")
    public CommonResult selectPage(@Validated @RequestBody ResourceQueryDto param) {
        return CommonResult.succeed(CommonPageBuilder.build(service.page(TransformUtils.copy(param, TResource.class)), ResourcePageVo.class));
    }
}
